﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using NHibernate;
using NHibernate.Linq;

namespace Barry.BarbeSystem.Business
{
    public class Repository<T> : IRepository<T> where T : class
    {


        public virtual IQueryable<T> GetAll()
        {
            ISession session = NHibernateHelper.OpenSession();
            var result = session.Query<T>();
            return result;
        }

        public virtual object Save(T model)
        {
            ISession session = NHibernateHelper.OpenSession();
            var result= session.Save(model);
            session.Flush();
            return result;

        }

        public virtual T Get(object id)
        {
            ISession sesion = NHibernateHelper.OpenSession();
            return sesion.Get<T>(id);
        }

        public virtual void Update(T entity)
        {
            ISession sesion = NHibernateHelper.OpenSession();
             if (!sesion.Contains(entity))
             {
                 entity = sesion.Merge(entity);
             }
           
            sesion.Update(entity);
            sesion.Flush();
        }

        public virtual void Delete(T entity)
        {
            ISession sesion = NHibernateHelper.OpenSession();
            sesion.Delete(entity);
            sesion.Flush();
        }

        public int ListCount()
        {
            ISession session = NHibernateHelper.OpenSession();

           return  session.Query<T>().Count();
        }
    }

}